package com.hs.ding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.ding.mapper.NeedMapper;
import com.hs.ding.mapper.VehicleMapper;
import com.hs.ding.pojo.Need;
import com.hs.ding.pojo.Transport;
import com.hs.ding.mapper.TransportMapper;
import com.hs.ding.pojo.Vehicle;
import com.hs.ding.service.ITransportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.dto.PageBean;
import com.hs.dto.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ding
 * @since 2022-06-18
 */
@Service
public class TransportServiceImpl extends ServiceImpl<TransportMapper, Transport> implements ITransportService {

    @Autowired
    private NeedMapper needMapper;

    @Autowired
    private VehicleMapper vehicleMapper;

    @Autowired
    private TransportMapper transportMapper;



    @Override
    public ResultBean findAllTransport(Integer currentPage, Integer pageSize,String tOrderId) {

        //查询状态为0
        QueryWrapper<Transport> qw = new QueryWrapper<>();
        qw.eq("t_status",0);

        //运输单号条件不为空，根据条件查询指定对象
        if (StringUtils.hasText(tOrderId)) {
            qw.like("t_order_id",tOrderId);
        }

        //分页构造器
        IPage<Transport> page = new Page<>(currentPage,pageSize);
        IPage<Transport> p = transportMapper.selectPage(page, qw);

        // 分页的的记录
        List<Transport> records = p.getRecords();

        for (Transport transport : records) {

            Vehicle vehicle = vehicleMapper.selectById(transport.getvOrderId());
            Need need = needMapper.selectById(vehicle.getOrderId());

            transport.setNeed(need);
        }

        Long total = p.getTotal();
        PageBean pageBean = new PageBean();
        pageBean.setList(records);
        pageBean.setTotal(total.intValue());

        ResultBean ok = new ResultBean();
        ok.setData(pageBean);

        return ok;
    }

    /**
     *
     *  01 通过
     *      tOrderId  查询 v_order_id
     *      通过 v_orderId 查询 vehicle 修改状态
     *
     *  02 通过 tOrderId 删除 当前的这条数据
     *
     * @param tOrderId
     * @return
     */
    @Override
    public ResultBean deleteTransport(String tOrderId) {

        // 查询操作 拿到 v_orderId
        QueryWrapper<Transport> qr = new QueryWrapper<>();
        qr.eq("t_order_id",tOrderId);
        Transport transport = this.getOne(qr);
        String v_orderId = transport.getvOrderId();

        Vehicle vehicle = new Vehicle();
        vehicle.setvOrderId(v_orderId);
        vehicle.setvStatus(0);
        // 修改操作
        int i = vehicleMapper.updateById(vehicle);

        if (i > 0 ){
            boolean b = this.removeById(tOrderId);
            return b ? ResultBean.OK() : ResultBean.ERROR();
        }
        return ResultBean.ERROR();
    }


}
